package com.xjtu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xjtu.dto.assessment.AssessmentDto;
import com.xjtu.dto.registration.CapabilityAssessmentDto;
import com.xjtu.dto.registration.RegistrationDto;
import com.xjtu.entity.CapabilityAssessment;
import com.xjtu.entity.Elderly;
import com.xjtu.entity.Registration;
import com.xjtu.mapper.CapabilityAssessmentMapper;
import com.xjtu.mapper.ElderlyMapper;
import com.xjtu.mapper.RegistrationMapper;
import com.xjtu.result.PageResult;
import com.xjtu.result.Result;
import com.xjtu.service.RegistrationService;
import com.xjtu.vo.ElderlyVo;
import com.xjtu.vo.baseInfo.AssessmentReportVo;
import com.xjtu.vo.medication.MedicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RegistrationImpl implements RegistrationService {

    @Autowired(required = false)
    RegistrationMapper registrationMapper;

    @Autowired(required = false)
    CapabilityAssessmentMapper capabilityAssessmentMapper;

    @Autowired(required = false)
    ElderlyMapper elderlyMapper;
    @Override
    public Result insertNew(RegistrationDto registrationDto) {
        Elderly elderly = new Elderly();
        elderly.setName(registrationDto.getName());
        elderly.setCreateTime(registrationDto.getDate());
        elderly.setPhone(registrationDto.getPhone());
        elderly.setStatus(1);
        elderly.setUpdateTime(registrationDto.getDate());

        elderlyMapper.insert(elderly);
        if(elderly.getId() == 0) {
            log.error("插入失败");
            return Result.error("插入失败");
        }
        registrationDto.setElderId(elderly.getId());
        Registration registration = new Registration();
        BeanUtils.copyProperties(registrationDto, registration);
        log.info("{}", registration);
        registrationMapper.insert(registration);
        if(registration.getId() == 0) {
            log.error("插入失败");
            return Result.error("插入失败");
        }

        log.info("插入成功");
        return Result.success(elderly.getId());
    }

    @Override
    public Result insertAssessment(CapabilityAssessmentDto capabilityAssessmentDto) {

        CapabilityAssessment capabilityAssessment = new CapabilityAssessment();
        BeanUtils.copyProperties(capabilityAssessmentDto, capabilityAssessment);

        capabilityAssessmentMapper.insert(capabilityAssessment);
        if(capabilityAssessment.getId() == 0) {
            log.error("插入失败");
            return Result.error("插入失败");
        }

        log.info("插入成功");
        return Result.success();
    }

    @Override
    public PageResult getList(int page, int perPage, String elderlyName) {
        PageHelper.startPage(page, perPage);
        List<ElderlyVo> elderlys = elderlyName.equals("null") ? elderlyMapper.getRegrationElderly() : elderlyMapper.getRegrationElderlyByName(elderlyName);

        PageInfo<ElderlyVo> pageInfo = new PageInfo<>(elderlys);
        System.out.println("pageInfo = " + pageInfo);

        long total = pageInfo.getTotal();
        List<ElderlyVo> records = pageInfo.getList();
        return new PageResult(total, records);
    }
}
